Homework 11

In [1]:
import numpy as np
import matplotlib.pyplot as plt
import sympy as sy
import utils as utils
from fractions import Fraction

from func import Func
from globalsearch import NaiveRandomSearch, SimulatedAnnealing, create_animation
from IPython.display import display, HTML

# Inline plotting
%matplotlib inline

# Make sympy print pretty math expressions
sy.init_printing()

plt.rc('animation', html='jshtml')

Example 14.3


Exercise 14.2

In [2]:
def create_f142():
    x, y = sy.symbols('x, y')
    term1 = 3*(1-x)**2 * sy.exp(-x**2 - (y+1)**2)
    term2 = 10*(x/5 - x**3 - y**5) * sy.exp(-x**2 - y**2)
    term3 = sy.exp(-(x+1)**2 - y**2) / 3
    f = term1 - term2 - term3
    return Func(f, (x,y))

f142 = create_f142()
f142
$$\left[\begin{matrix}3 \left(- x + 1\right)^{2} e^{- x^{2} - \left(y + 1\right)^{2}} - \left(- 10 x^{3} + 2 x - 10 y^{5}\right) e^{- x^{2} - y^{2}} - \frac{e^{- y^{2} - \left(x + 1\right)^{2}}}{3}\end{matrix}\right]$$
Out[2]:

In [3]:
fig, ax = f142.plot_contour(x_limit=(-3, 3), levels=30)

In [5]:
nrs = NaiveRandomSearch(f142, feasible_set=[(-3, 3), (-3, 3)])
result = nrs.run(initial_point=(0.5, -0.5), alpha=0.1, max_iterations=100, verbose=False)
anim = create_animation(fig, ax, result)
anim
Out[5]:


Once Loop Reflect
In [6]:
# anim.save('figures/homework-11/animation-naive-random-search.gif', writer='imagemagick', fps=5)

Simulated Annealing

In [7]:
fig, ax = f142.plot_contour(x_limit=(-3, 3), levels=30)
In [8]:
sa = SimulatedAnnealing(f142, feasible_set=[(-3, 3), (-3, 3)])
result = sa.run(
    initial_point=(0, 2), 
    alpha=3, 
    gamma=2,
    max_tries=30,
    max_iterations=300, 
    verbose=False
)
anim = create_animation(fig, ax, result)
anim
Out[8]:


Once Loop Reflect

Exercise 14.3

In [9]:
from globalsearch_pso import PSO, create_animation
In [10]:
def create_peaks_func():
    x, y = sy.symbols('x, y')
    term1 = 3*(1-x)**2 * sy.exp(-x**2 - (y+1)**2)
    term2 = 10*(x/5 - x**3 - y**5) * sy.exp(-x**2 - y**2)
    term3 = sy.exp(-(x+1)**2 - y**2) / 3
    f = term1 - term2 - term3
    return Func(f, (x,y))

f143 = create_peaks_func()
f143
$$\left[\begin{matrix}3 \left(- x + 1\right)^{2} e^{- x^{2} - \left(y + 1\right)^{2}} - \left(- 10 x^{3} + 2 x - 10 y^{5}\right) e^{- x^{2} - y^{2}} - \frac{e^{- y^{2} - \left(x + 1\right)^{2}}}{3}\end{matrix}\right]$$
Out[10]:

In [11]:
# For reproducibility, we seed the random number generator
np.random.seed(42)
In [12]:
pso = PSO(f143, feasible_set=[(-3, 3), (-3, 3)], num_particles=20)
result = pso.run(w=0.8, c1=2, c2=0.1, max_iterations=20)
In [13]:
fig, ax = f143.plot_contour()
best_point = result['best_point']
ax.scatter(best_point[0], best_point[1], color='#ffffff')
Out[13]:
<matplotlib.collections.PathCollection at 0x7fada329af98>
In [14]:
create_animation(fig, ax, result, show_annotations=False)
Out[14]:


Once Loop Reflect